<?php

namespace app\model;

use think\Model;

class Article extends Model
{
    //文章表

    /**
     * 添加文章
     * @param $info
     * @return array
     */
    public function saveArticle( $info )
    {

        if ( $info[ 'title' ] ) {

            if ( $info[ 'content' ] ) {

                //标题字数是否超出数据库限制
                if ( iconv_strlen( $info[ 'title' ], 'utf-8' ) <= 50 ) {

                    //拼接标签数组
                    if ( isset($info[ 'label' ])) {

                        $label = implode( ',', $info[ 'label' ] );

                    } else {

                        $label = '';

                    }

                    //存储时间
                    $date = date( 'Y-m-d H:i:s' );

                    $instert = [

                        'cd' => $date,

                        'md' => $date,

                        'status' => 1,

                        'like_num' => 0,

                        'browse_num' => 0,

                        'title' => $info[ 'title' ],

                        'content' => $info[ 'content' ],

                        'cid' => $info[ 'class' ],

                        'label' => $label

                    ];

                    $re = $this->save( $instert );

                    if ( $re ) {

                        $return = [ 'status' => true ];

                    } else {

                        $return = [ 'status' => false, 'info' => '失败了,要不再试一次' ];

                    }

                } else {

                    $return = [ 'status' => false, 'info' => '文章标题别超过50个字' ];

                }

            } else {

                $return = [ 'status' => false, 'info' => '文章内容都没有,想做标题党吗?' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '标题都不写,文章谁看啊?' ];

        }

        return $return;

    }
}
